﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MetaSharp.Transformation.Patterns
{
    class ValuePattern<T> : Pattern
    {
        public T Value { get; private set; }
        public ValuePattern(T value)
            : base(PatternKind.Value)
        {
            this.Value = value;
        }

        public override PatternMatch Compile()
        {
            return s => Target(s, Value);
        }

        private static async Task<IMatch> Target(IScope scope, T value)
        {
            if (!scope.Stream.IsEmpty)
            {
                var next = scope.Stream.Next();
                if (object.Equals(next.Current, value))
                    return new Match(
                        value,
                        scope,
                        scope.SetStream(next));
            }

            return await Transformation.Match.Fail(scope);
        }
    }
}
